package com.roin.learn.activiti.service.impl;

import com.github.pagehelper.PageHelper;
import com.roin.learn.activiti.dao.PositionMapper;
import com.roin.learn.activiti.entity.Page;
import com.roin.learn.activiti.entity.Position;
import com.roin.learn.activiti.entity.PositionExample;
import com.roin.learn.activiti.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


@Service
public class PositionServiceImpl implements PositionService {
	@Autowired
	private PositionMapper positionMapper;

	public Position getById(Integer id) {
		return positionMapper.selectByPrimaryKey(id);

	}

	public Page<Position> getPage(int pageNum, int pageSize, Position position) {
		PageHelper.startPage(pageNum,pageSize);
		PositionExample example = new PositionExample();
		if (position!=null){
			PositionExample.Criteria criteria = example.createCriteria();
			if (position.getPositionNo()!=null)
				criteria.andPositionNoLike(position.getPositionNo());
			if (position.getPositionMc()!=null)
				criteria.andPositionMcLike(position.getPositionMc());

		}
		return new Page<Position>(positionMapper.selectByExample(example));
	}

	public int update(Position position) {
		return positionMapper.updateByPrimaryKeySelective(position);
	}

	@Override
	public int add(Position position) {
		return positionMapper.insert(position);
	}

	@Override
	public int deleteByIds(Integer[] ids) {
		PositionExample example = new PositionExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		return positionMapper.deleteByExample(example);
	}

	@Override
	public List<Position> getAll() {
		return positionMapper.selectByExample(null);
	}

}
